<?php

require_once WACT_ROOT . '/db/db.inc.php';
require_once APP_ROOT.'model/common/utils.php';
require_once APP_ROOT.'model/common/site.php';


class Scrolling {

	function &getOrientations() {
    	$orientations = array('0'=>'Verticlal','1'=>'Horizontal');

    	return $orientations;
    }

    function getOrientationName($id) {
    		$orients =& Scrolling::getOrientations();
    		$name = $orients[$id];
    		return $name;
    }


    function insertItem(&$data) {
        $site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
        $Record =& DBC::NewRecord($data);
        return $Record->insertId('sport_scrolling_news',
            array('headline','date_posted', 'expiration_date', 'user_id', 'summary',
            'enabled', 'category_id','category2_id','orientation','photo_url','site_id'),
            'id');
    }

    function &getRecord($id) {
        return DBC::FindRecord("SELECT U.*, S.*
        FROM sport_scrolling_news S
        left outer join sport_users U on S.user_id = U.user_id
        WHERE S.id = ". DBC::makeLiteral($id));
    }

    function updateItem($id, &$data) {
        $site_id = Site::getSiteID();
    	$data->set('site_id',$site_id);
        $Record =& DBC::NewRecord($data);
        $Record->update('sport_scrolling_news',
            array('headline', 'expiration_date', 'summary',
            'enabled', 'category_id','category2_id','orientation','photo_url','site_id'),
            "id=". DBC::makeLiteral($id));
    }


    function delete($id) {
        DBC::execute("DELETE FROM sport_scrolling_news WHERE id=". DBC::makeLiteral($id));
    }

     function &getScrollingList(&$Pager, &$request) {
     	$site_id = Site::getSiteID();
     	$sql = "select S.*, U.*, C1.name as category1, C2.name as category2
     	from sport_scrolling_news S
     	left outer join sport_users U on S.user_id = U.user_id
     	left outer join sport_categories C1 on S.category_id = C1.category_id
     	left outer join sport_categories C2 on S.category2_id = C2.category_id
     	where 1=1 ".Scrolling::getScrollingFilter($request)."
        and S.site_id = ".DBC::makeLiteral($site_id)." ".
     	" order by ".Scrolling::getScrollingOrder($request);
     	//print $sql;
        return DBC::NewPagedRecordSet($sql, $Pager);
     }

     function getScrollingFilter(&$request) {
      	$where = '';
      	return $where;
      }

      function getScrollingOrder(&$request) {
      	$sortby = 'S.date_posted';

      	return $sortby;
      }

      function &getRendomRecord($sport,$community) {
      	$ar =& Scrolling::getRandomRecord($sport,$community);
      	return $ar;
      }


      function &getRandomRecord($sport,$community) {
      	$site_id = Site::getSiteID();
		$query="select * from sport_scrolling_news where
		enabled=1 and
		photo_url is NOT NULL
		and photo_url<>''
        and expiration_date>=now()
        and site_id = ".DBC::makeLiteral($site_id)." ";
		if($sport)
			$query.=" and category_id=$sport ";
		if($community) $query.=" and category2_id=$community ";
		$dataset=DBC::NewRecordSet($query);
		$count=$dataset->getTotalRowCount();
		$number=rand(0,$count-1);
		$dataset->reset();
		$i=-1;
		if($count>0){
		do{
			$i++;
			$dataset->next();
		} while($i!=$number) ;
		$ar=$dataset->export();
		return $ar;
		}else {
			$ar = null;
		    // try to find the record without categories now
            if ($sport && $community)     {
            	$ar = Scrolling::getRandomRecord($sport,null);
            	if (!$ar) $ar = Scrolling::getRandomRecord(null,$community);
                if (!$ar) $ar = Scrolling::getRandomRecord(null,null);
            } else if ($sport) {
              	$ar = Scrolling::getRandomRecord(null,null);
            } else if ($community) {
              	$ar = Scrolling::getRandomRecord(null,null);
            }

			return $ar;
		}
	}
}

?>
